const Admin = require("../model/admin.model");
const User = require("../model/user.model");
const Collect = require("../model/collect.model");
const { getAllCollectId } = require("./collect.service");
const { getTopic } = require("./collect.service");
const { getComments } = require("./topic.service");
class AdminService {
  async adminLogin(user_name, password) {
    const res = await Admin.findOne({
      attributes: ["user_name", "password"],
      where: {
        user_name,
        password,
      },
    });
    console.log(res);

    return res ? res.dataValues : null;
  }

  async findAllUserInfo() {
    const res = await User.findAll({
      attributes: [
        "id",
        "user_name",
        "password",
        "phone_number",
        "user_avatar",
        "user_gendar",
        "user_signature",
        "createdAt",
        "updatedAt",
      ],
    });
    return res ? res : null;
  }

  async findAllCollect() {
    let res = await Collect.findAll({
      attributes: ["id", "user_id", "collect_topic_id"],
    });
    let collect_arr = [];
    for (let i = 0; i < res.length; i++) {
      collect_arr[i] = res[i].dataValues;
    }
    console.log("collect_arr开始啦------------------------");
    console.log(collect_arr);
    console.log("collect_arr结束啦------------------------");
    let mes = [];
    mes[0].user_id = collect_arr[0].user_id;
    for (let i = 1; i < collect_arr.length; i++) {
      let exist = false;
      for (let j = 0; j < mes.length; j++) {
        if (mes[j].user_id == collect_arr[i].user_id) {
          exist = true;
        }
      }
      if (!exist) {
        mes[mes.length].user_id = collect_arr[i].user_id;
      }
    }

    console.log("mes开始啦。。。。。。。。。。。。。。。");
    console.log(mes);
    console.log("mes结束啦.............................");
    for (let i = 0; i < mes.length; i++) {
      const collected_id_arr = await getAllCollectId(mes[i]);
      for (let j = 0; j < collect_arr.length; j++) {
        const topic_content = await getTopic(collected_id_arr[j]);
        mes[i].topic[j] = topic_content;
        mes[i].topic[j].comment_mes = [];
        const comment_mes = await getComments(collected_id_arr[j]);
        if (comment_mes.length > 0) {
          for (let k = 0; k < comment_mes.length; k++) {
            mes[i].topic[j].comment_mes[k] = comment_mes[k].dataValues;
          }
        }
      }
    }
    console.log("再次打印mes!!!!!!!!!!");
    console.log(mes);
    console.log("结束打印mes！！！！！！");
    /*  let collect_arr = [];
    collect_arr[0].user_id = res[0].dataValues.user_id;
    collect_arr[0].collets = [];
    collect_arr[0].collets[0] = res[0].dataValues.collect_topic_id;
    console.log("collect_Arr开始00000000000000000");
    console.log(collect_arr);
    console.log("collect_Arr结束11111111111111111");
    for (let i = 1; i < res.length; i++) {
      let exist = false;
      for (let j = 0; j < collect_arr.length; j++) {
        if (res[i].dataValues.user_id == collect_arr[j].user_id) {
          console.log(res[i].dataValues.user_id, collect_arr[j].user_id);
          exist = true;
          collect_arr[j].collets[collect_arr[j].collets.length] =
            res[i].dataValues.collect_topic_id;
        }
      }

      if (!exist) {
        collect_arr[collect_arr.length].user_id = res[i].dataValues.user_id;
        collect_arr[collect_arr.length].collets = [];
        collect_arr[collect_arr.length].collets[0] =
          res[i].dataValues.collect_topic_id;
      }
    }
    console.log("collect_arr开始----------");
    console.log(collect_arr);
    console.log("collect_arr结束----------"); */
    return mes;
  }
}

module.exports = new AdminService();
